Dynomotion

Group: DynoMotion Message: 8847 From: ricochetproducts Date: 1/2/2014
Subject: Help with push button switches
Tom,  would you help please?  For some reason I can not figure this out.  I want a push button (momentary) (1038) to turn on (149) & output (50) then I want to push it again clear these outputs.   Would the same logic work with a toggle switch? If not, do you have an example of that also

   I have been unable to find an example. Thank you for all your help!
Group: DynoMotion Message: 8850 From: ricochetproducts Date: 1/3/2014
Subject: Re: Help with push button switches

Can or will anybody help with this?

Group: DynoMotion Message: 8851 From: Tom Kerekes Date: 1/3/2014
Subject: Re: Help with push button switches
Attachments :
    Hi Steve,

    Try the attached.  It debounces the momentary input and when first is established high (Debounce returns 1) then it checks if your output is currently high or low, and then commands to the opposite state.

    Regards
    TK

    Group: DynoMotion Message: 8852 From: Tom Kerekes Date: 1/3/2014
    Subject: Re: Help with push button switches
    Hi Steve,

    Not sure why it is taking Yahoo so long to post the attachment.  Here it is pasted below.

    Regards
    TK


    #include "KMotionDef.h"


    // function prototypes for compiler
    int Debounce(int n, int *cnt, int *last, int *lastsolid);

    // state variables for switch debouncing
    int mlast=0,mlastsolid=-1,mcount=0;

    main()
    {
        for (;;) // loop forever
        {
            WaitNextTimeSlice();
           
            // Debounce Momentary Push Button
            if  (Debounce(ReadBit(1038),&mcount,&mlast,&mlastsolid) == 1)
            {
                if (ReadBit(149))
                {
                    ClearBit(149);
                    ClearBit(50);
                }
                else
                {
                    SetBit(149);
                    SetBit(50);
                }
            }
        }
    }

    // Debounce a bit
    //
    // return 1 one time when first debounced high
    // return 0 one time when first debounced low
    // return -1 otherwise
    #define DBTIME 300

    int Debounce(int n, int *cnt, int *last, int *lastsolid)
    {
        int v = -1;
       
        if (n == *last)  // same as last time?
        {
            if (*cnt == DBTIME-1)
            {
                if (n != *lastsolid)
                {
                    v = *lastsolid = n;  // return debounced value
                }
            }
            if (*cnt < DBTIME)    (*cnt)++;
        }
        else
        {
            *cnt = 0;  // reset count
        }
        *last = n;
        return v;
    }

    Group: DynoMotion Message: 8853 From: Steve Klemp Date: 1/3/2014
    Subject: Re: Help with push button switches
    Hi Tom,  Thank You for your help!  I think I understand now,  I will try it out in the morning.  I am a ladder logic guy and I am a little slow with this.   
     
    .....Steve
                
     


    On Friday, January 3, 2014 6:03 PM, Tom Kerekes <tk@...> wrote:
     
    Hi Steve,

    Not sure why it is taking Yahoo so long to post the attachment.  Here it is pasted below.

    Regards
    TK


    #include "KMotionDef.h"


    // function prototypes for compiler
    int Debounce(int n, int *cnt, int *last, int *lastsolid);

    // state variables for switch debouncing
    int mlast=0,mlastsolid=-1,mcount=0;

    main()
    {
        for (;;) // loop forever
        {
            WaitNextTimeSlice();
           
            // Debounce Momentary Push Button
            if  (Debounce(ReadBit(1038),&mcount,&mlast,&mlastsolid) == 1)
            {
                if (ReadBit(149))
                {
                    ClearBit(149);
                    ClearBit(50);
                }
                else
                {
                    SetBit(149);
                    SetBit(50);
                }
            }
        }
    }

    // Debounce a bit
    //
    // return 1 one time when first debounced high
    // return 0 one time when first debounced low
    // return -1 otherwise
    #define DBTIME 300

    int Debounce(int n, int *cnt, int *last, int *lastsolid)
    {
        int v = -1;
       
        if (n == *last)  // same as last time?
        {
            if (*cnt == DBTIME-1)
            {
                if (n != *lastsolid)
                {
                    v = *lastsolid = n;  // return debounced value
                }
            }
            if (*cnt < DBTIME)    (*cnt)++;
        }
        else
        {
            *cnt = 0;  // reset count
        }
        *last = n;
        return v;
    }